package com.walkline.travelassistant.dao;

import java.util.Vector;

import org.json.me.JSONArray;
import org.json.me.JSONException;
import org.json.me.JSONObject;

import com.walkline.travelassistant.TravelAssistantException;
import com.walkline.travelassistant.TravelAssistantSDK;
import com.walkline.travelassistant.inf.Step;
import com.walkline.travelassistant.inf.StepWrapper;
import com.walkline.util.Function;

public class TravelAssistantStepWrapper extends TravelAssistantObject implements StepWrapper
{
	private Vector _step_list = new Vector();
	private int _step_count = 0;

	public TravelAssistantStepWrapper(TravelAssistantSDK travelAssistant, JSONObject jsonObject) throws TravelAssistantException
	{
		super(travelAssistant, jsonObject);

		JSONObject stepWrapper = jsonObject;
		if (stepWrapper != null)
		{
			JSONArray steps = stepWrapper.optJSONArray("steps");
			if (steps != null)
			{
				JSONObject stepObject = null;
				Step step;

				_step_count = steps.length();
				for (int i=0; i<_step_count; i++)
				{
					try {
						if (steps.get(i).toString().startsWith("["))
						{
							JSONArray stepArray = (JSONArray) steps.get(i);
							if (stepArray != null) {stepObject = (JSONObject) stepArray.get(0);}							
						} else {
							stepObject = (JSONObject) steps.get(i);							
						}

						step = new TravelAssistantStep(travelAssistant, stepObject);
						if (step != null) {_step_list.addElement(step);}
					} catch (JSONException e) {Function.errorDialog(e.toString());}
				}
			}
		}
	}

	public Vector getSteps() {return _step_list;}

	public int getStepsCount() {return _step_count;}
}